/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lsi.sgb.gui;

import br.com.lsi.sgb.dao.ContatoDao;
import br.com.lsi.sgb.dao.FuncionarioDao;
import br.com.lsi.sgb.dao.UsuarioDao;
import br.com.lsi.sgb.model.Funcionario;
import br.com.lsi.sgb.util.Contato;
import br.com.lsi.sgb.util.Usuario;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author DiegoNicacio
 */
public class AddFuncionario extends javax.swing.JDialog {

    /**
     * Creates new form AddFuncionario
     */
    
    private boolean flag;
    private Funcionario funcionario;
    private Usuario usuario; 
    private Contato contato;
    
    public AddFuncionario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setLocationRelativeTo(this);
        initComponents();
        this.flag = false;
    }
    
     public AddFuncionario(java.awt.Frame parent, boolean modal, Funcionario funcionario, Contato contato, Usuario usuario) {
        super(parent, modal);
        setLocationRelativeTo(this);
        initComponents();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
        this.contato = contato;
        this.usuario = usuario;
        this.funcionario = funcionario;
        campoNome.setText(funcionario.getNome());
        campoCpf.setText(funcionario.getCpf() + "");
        campoIdentidade.setText(funcionario.getIdentidade() + "");
        String data = sdf.format(funcionario.getDataNascimento().getTime());
        campoData.setText(data + "" );
        this.flag = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelNome = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        labelCpf = new javax.swing.JLabel();
        campoCpf = new javax.swing.JTextField();
        labelIdentiade = new javax.swing.JLabel();
        campoIdentidade = new javax.swing.JTextField();
        labelData = new javax.swing.JLabel();
        campoData = new javax.swing.JTextField();
        botaoSalvar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        textCel = new javax.swing.JTextField();
        textTel = new javax.swing.JTextField();
        labelMail = new javax.swing.JLabel();
        textMail = new javax.swing.JTextField();
        labelCel = new javax.swing.JLabel();
        labelTel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        labelLogin = new javax.swing.JLabel();
        campoLogin = new javax.swing.JTextField();
        labelSenha = new javax.swing.JLabel();
        campoSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        labelNome.setText("Nome:");

        labelCpf.setText("CPF: ");

        labelIdentiade.setText("RG: ");

        labelData.setText("Dt. Nascimento");

        botaoSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/lsi/sgb/imagens/1294_16x16.png"))); // NOI18N
        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        botaoCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/lsi/sgb/imagens/cancel.png"))); // NOI18N
        botaoCancelar.setText("Cancelar ");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        textCel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textCelActionPerformed(evt);
            }
        });

        labelMail.setText("Email:");

        textMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textMailActionPerformed(evt);
            }
        });

        labelCel.setText("Celular:");

        labelTel.setText("Telefone:");

        labelLogin.setText("Login: ");

        labelSenha.setText("Senha: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jSeparator2)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(botaoCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(labelTel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textTel, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(labelCel)
                                    .addGap(13, 13, 13)
                                    .addComponent(textCel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(labelNome)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(labelCpf)
                                        .addComponent(labelIdentiade))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(campoCpf)
                                        .addComponent(campoIdentidade, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(15, 15, 15)
                                    .addComponent(labelData)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(labelMail)
                                    .addGap(22, 22, 22)
                                    .addComponent(textMail, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelLogin)
                                .addGap(18, 18, 18)
                                .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(labelSenha)
                                .addGap(18, 18, 18)
                                .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 13, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelCpf)
                        .addGap(24, 24, 24)
                        .addComponent(labelIdentiade))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelData)
                            .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(campoIdentidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelMail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTel)
                    .addComponent(textTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCel))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelSenha))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelLogin)
                        .addComponent(campoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(botaoCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
         FuncionarioDao dao = new FuncionarioDao();
         UsuarioDao userDao = new UsuarioDao();
         ContatoDao contatoDao = new ContatoDao();
        Funcionario f = new Funcionario();
        Contato c = new Contato();
        Usuario u = new Usuario();
        if (!flag) {
            f.setNome(campoNome.getText());
            f.setCpf(Integer.valueOf(campoCpf.getText()));
            f.setIdentidade(Integer.valueOf(campoIdentidade.getText()));          
            c.setCelular(textCel.getText());
            c.setEmail(textMail.getText());
            c.setTelefone(textTel.getText());
            u.setLogin(campoLogin.getText());
            u.setSenha(campoSenha.getText());
              try{
                    String data = campoData.getText(); 
                    SimpleDateFormat sdf; 
                    Date date;
                    sdf = new SimpleDateFormat("dd/MM/yyyy"); 
                    date = (Date)sdf.parse(data);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    f.setDataNascimento(cal);
                }catch (ParseException e){
                    e.printStackTrace();
                }
            contatoDao.addContato(c);
            userDao.addUsuario(u);
            f.setContato(c);
            f.setUsuario(u);
            dao.addFuncionario(f);
            JOptionPane.showMessageDialog(null, "Funcionario cadastrado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            funcionario.setNome(campoNome.getText());
            funcionario.setCpf(Integer.valueOf(campoCpf.getText()));
            funcionario.setIdentidade(Integer.valueOf(campoIdentidade.getText()));
            contatoDao.atualizarContato(contato);
            userDao.atualizarUsuario(usuario);
            funcionario.setContato(contato);
            funcionario.setUsuario(usuario);
              try{
                    String data = campoData.getText(); 
                    SimpleDateFormat sdf; 
                    Date date;
                    sdf = new SimpleDateFormat("dd/MM/yyyy"); 
                    date = (Date)sdf.parse(data);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    f.setDataNascimento(cal);
                }catch (ParseException e){
                    e.printStackTrace();
                }
             dao.atualizarFuncionario(funcionario);
            JOptionPane.showMessageDialog(null, "Funcionario editado com sucesso.", "Ok", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        }
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_botaoCancelarActionPerformed

    private void textCelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textCelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textCelActionPerformed

    private void textMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textMailActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textMailActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddFuncionario dialog = new AddFuncionario(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoCpf;
    private javax.swing.JTextField campoData;
    private javax.swing.JTextField campoIdentidade;
    private javax.swing.JTextField campoLogin;
    private javax.swing.JTextField campoNome;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelCel;
    private javax.swing.JLabel labelCpf;
    private javax.swing.JLabel labelData;
    private javax.swing.JLabel labelIdentiade;
    private javax.swing.JLabel labelLogin;
    private javax.swing.JLabel labelMail;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JLabel labelTel;
    private javax.swing.JTextField textCel;
    private javax.swing.JTextField textMail;
    private javax.swing.JTextField textTel;
    // End of variables declaration//GEN-END:variables
}
